﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;
using C1.Phone.Calendar;
using System.Windows.Data;

namespace ControlExplorer.Controls.Calendar
{
    /// <summary>
    /// Binds calendar to custom data and use custom BoldedDaySlotTemplate to show information about events. 
    /// Double tap on day to review detailed information about events.
    /// </summary>
    public partial class CustomData : UserControl
    {
        private AppointmentCollection _appointments = new AppointmentCollection();
        private int _appNumber = 1;

        public CustomData()
        {
            InitializeComponent();

            // bind C1Calendar to the AppointmentCollection
            cal1.StartTimePath = "Start";
            cal1.EndTimePath = "End";
            cal1.DataSource = _appointments;

            // create some default appointments
            CreateTestEvent(DateTime.Today.AddDays(1));
            CreateTestEvent(DateTime.Today.AddDays(2));
            CreateTestEvent(DateTime.Today.AddDays(12));
            CreateTestEvent(DateTime.Today.AddDays(10));
            CreateTestEvent(DateTime.Today.AddDays(12));
            CreateTestEvent(DateTime.Today.AddDays(15));
            CreateTestEvent(DateTime.Today.AddDays(-21));
            CreateTestEvent(DateTime.Today.AddDays(-11));
            CreateTestEvent(DateTime.Today.AddDays(-3));
            CreateTestEvent(DateTime.Today.AddDays(2));
            CreateTestEvent(DateTime.Today.AddDays(-21));
            CreateTestEvent(DateTime.Today.AddDays(12));
        }

        private void CreateTestEvent(DateTime start)
        {
            Appointment app = new Appointment();
            app.Start = start;
            app.Duration = TimeSpan.FromDays(1);
            app.Subject = "event " + _appNumber.ToString();
            _appNumber++;
            _appointments.Add(app);
        }

        private void cal1_DoubleTap(object sender, GestureEventArgs e)
        {
            // show the list of appointments for the bolded day
            FrameworkElement fel = e.OriginalSource as FrameworkElement;
            if (fel != null)
            {
                DaySlot slot = fel.DataContext as DaySlot;
                if (slot != null)
                {
                    DateTime date = slot.Date;
                    string message = date.ToShortDateString();
                    if (slot.DataSource == null || slot.DataSource.Count == 0)
                    {
                        message += "\r\nNo events for this day.";
                    }
                    else
                    {
                        foreach (Appointment app in slot.DataSource)
                        {
                            message += "\r\n" + app.Subject;
                        }
                    }
                    MessageBox.Show(message);
                }
            }
        }
    }

    [DataContract(Name = "Appointment", Namespace = "http://www.componentone.com")]
    public class Appointment : INotifyPropertyChanged
    {
        public Appointment()
        {
            Id = Guid.NewGuid();
        }
        [DataMember]
        public Guid Id { get; private set; }

        private string _subject = "";
        [DataMember]
        public string Subject
        {
            get { return _subject; }
            set
            {
                if (_subject != value)
                {
                    _subject = value;
                    OnPropertyChanged("Subject");
                }
            }
        }

        private string _location = "";
        [DataMember]
        public string Location
        {
            get { return _location; }
            set
            {
                if (_location != value)
                {
                    _location = value;
                    OnPropertyChanged("Location");
                }
            }
        }

        private DateTime _start;
        [DataMember]
        public DateTime Start
        {
            get { return _start; }
            set
            {
                if (_start != value)
                {
                    _start = value;
                    OnPropertyChanged("Start");
                }
            }
        }

        [DataMember]
        public DateTime End
        {
            get { return _start.Add(_duration); }
            set
            {
                if (value >= _start)
                {
                    Duration = (value.Subtract(_start));
                    OnPropertyChanged("End");
                }
            }
        }

        private TimeSpan _duration;
        public TimeSpan Duration
        {
            get { return _duration; }
            set
            {
                if (_duration != value)
                {
                    _duration = value;
                    OnPropertyChanged("Duration");
                }
            }
        }

        private string _description = "";
        [DataMember]
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    _description = value;
                    OnPropertyChanged("Description");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // inherit from ObservableCollection to allow auto refresh in calendar
    public class AppointmentCollection : ObservableCollection<Appointment>
    {
        public AppointmentCollection()
        {
        }
    }

    public class EventsToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            IList<object> list = value as IList<object>;
            if (list != null && list.Count > 0)
            {
                if (list.Count == 1)
                {
                    return ((Appointment)list[0]).Subject;
                }
                else
                {
                    return list.Count + " events";
                }
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
